﻿////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	SelezPoint
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;

using System.Windows.Forms;
using System.Data.SQLite;
using System.Globalization;

namespace Map_Fvg
{
    public partial class SelezPoint : Form
    {
        public static int iniz;

        public SelezPoint()
        {
            InitializeComponent();
        }

        private string[] codici = new string[200];
        private int num_codici;

        private void SelezPoint_Load(object sender, EventArgs e)
        {
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            find_codici();

            for (int j = 0; j < num_codici; ++j) comboBox1.Items.Add(codici[j]);
            string codice = string.Empty;

            if (iniz == 1)
            {
                label1.Text = "inserimento simbolo";
                button2.Text = "inserimento";
                button3.Visible = false;
                button3.Enabled = false;
                textBox6.Text = (ViewQ.nShapesP + 1).ToString();

                comboBox1.SelectedIndex = 0;
                codice = (string)comboBox1.SelectedItem;
                textBox1.Text = find_denominazione(codice);
                textBox2.Text = ViewQ.HitM.X.ToString();
                textBox3.Text = ViewQ.HitM.Y.ToString();
                textBox4.Text = "0";

            }
            else
            {
                label1.Text = "modifica simbolo";
                button2.Text = "modifica";
                button3.Visible = true;
                button3.Enabled = true;
                textBox5.Enabled = false;
                textBox6.Text = ViewQ.i_selected.ToString();

                int i_selected = ViewQ.i_selected;
                int pntSimb = ViewQ.vectShapesP[i_selected].pntSimb;
                codice = ViewQ.vectP[pntSimb].codice;
                int index = comboBox1.FindString(codice);
                comboBox1.SelectedIndex = index;

                textBox1.Text = find_denominazione(codice);
                textBox2.Text = ViewQ.vectShapesP[i_selected].XX.X.ToString();
                textBox3.Text = ViewQ.vectShapesP[i_selected].XX.Y.ToString();
                textBox4.Text = ViewQ.vectShapesP[i_selected].GMrotation.ToString(nfi);
                textBox5.Text = ViewQ.vectP[pntSimb].paramSimb;
            }

        }



               private void find_codici()
        {

            using (SQLiteConnection conn = SetConnection())
            {
                int ipnt = 0;
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    string ss = "select codice_point from point";

                    cmd.CommandText = ss;

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            codici[ipnt] = reader.GetString(0).ToString();
                            ++ipnt;
                        }
                    }
                }

                conn.Close();
                num_codici = ipnt;
            }
        }

               private string find_denominazione(string codice)
               {

                   using (SQLiteConnection conn = SetConnection())
                   {
                       string denominazione = string.Empty;
                       conn.Open();
                       using (SQLiteCommand cmd = new SQLiteCommand(conn))
                       {

                           string ss = "select * from point WHERE codice_point = '";
                           ss = string.Concat(ss, codice, "'");

                           //                           MessageBox.Show(ss);
                           cmd.CommandText = ss;

                           SQLiteDataReader reader = cmd.ExecuteReader();
                           if (reader.HasRows)
                           {
                               while (reader.Read())
                               {
                                   denominazione = reader.GetString(1).ToString();

                               }
                           }
                       }

                       conn.Close();

                       return (denominazione);
                   }
               }

               private SQLiteConnection SetConnection()
               {
                   string DataSource = "Data Source=";
                   DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
                   SQLiteConnection conn = new SQLiteConnection(DataSource);
                   return (conn);
               }




        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            int ipnt = 0;
            string codice = codici[comboBox1.SelectedIndex];
            int i = ViewQ.i_selected;
            bool bNew = false;
            if (i == -1) bNew = true;

            if (!bNew)
            {
                int pntSimb = ViewQ.vectShapesP[i].pntSimb;
                if (ViewQ.vectP[pntSimb].codice == codice)
                {
                    ipnt = pntSimb;
                    goto L20;
                }
            }

            int j;
            for (j = 0; j < ViewQ.nSimbP; ++j)
            {
                if (ViewQ.vectP[j].codice == codice) break;
            }
            if (j < ViewQ.nSimbP && !bNew)
            {
                ViewQ.vectShapesP[i].pntSimb = j;
                ipnt = j;
                goto L20;
            }
            if (ViewQ.nSimbP < ViewQ.max_nSimbP)
            {
                ipnt = ViewQ.nSimbP;
                ++ViewQ.nSimbP;
                ViewQ.vectP[ipnt].codice = codice;
                ViewQ.vectP[ipnt].visib = 1;
                ViewQ.vectP[ipnt].selected = 1;
            }
            else
            {
                MessageBox.Show("procedere al salvataggio");
                return;
            }
            if (i >= 0)
            {
                ViewQ.vectShapesP[i].pntSimb = ipnt;
                goto L20;
            }
            //      inserire un nuovo codice in ViewQ.vectT[]
            //
            if (ViewQ.nShapesP >= ViewQ.max_nPoints)
            {
                MessageBox.Show("procedere al salvataggio");
                return;
            }

            i = ViewQ.nShapesP;
            ++ViewQ.nShapesP;
            ViewQ.vectShapesP[i].pntSimb = ipnt;
            ViewQ.vectP[ipnt].classe = find_classe(codice);
            string line = FormIniz.nameElement;
            if (line.Length > 6) line = line = FormIniz.nameElement.Substring(0, 6);
            ViewQ.vectP[ipnt].elemento = line;

        L20: using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    cmd.CommandText = "select * from point where codice_point = '" + codice + "'";

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            ViewQ.vectP[ipnt].denom = reader.GetString(1).ToString();
                            string ss = reader.GetString(2).ToString();
                            ViewQ.vectP[ipnt].simbolo = int.Parse(ss);
                            ViewQ.vectP[ipnt].paramSimb = reader.GetString(3).ToString();

                            char[] delimiterChars = { ' ', ',' };
                        }
                    }
                    conn.Close();
                }
            }

            ViewQ.vectShapesP[i].GMrotation = float.Parse(textBox4.Text, nfi);
            ViewQ.vectShapesP[i].XX.X = int.Parse(textBox2.Text);
            ViewQ.vectShapesP[i].XX.Y = int.Parse(textBox3.Text);
            ViewQ.vectShapesP[i].modifica = 1;
            ViewQ.vectShapesP[i].selected = 1;
            if (bNew)
            {
                ViewQ.vectShapesP[i].edizione = 3;
                ViewQ.vectShapesP[i].modifica = 3;
            }
            else
            {
                ViewQ.vectShapesP[i].edizione = 2;
                if (ViewQ.vectShapesP[i].modifica == 0) ViewQ.vectShapesP[i].modifica = 1;
            }
            Save.modificaPunto = 1;

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            int i = ViewQ.i_selected;

            string ss = "si vuole cancellare il punto n°";
            ss = string.Concat(ss, i.ToString(), " ?");
            string caption = "avviso";

            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;

            result = MessageBox.Show(ss, caption, buttons);
            if (result == DialogResult.Yes)
            {
                ViewQ.vectShapesP[i].modifica = 2;
                Save.modificaPunto = 1;
                this.DialogResult = DialogResult.OK;
            }
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string codice = codici[comboBox1.SelectedIndex];
            textBox1.Text = find_denominazione(codice);
        }

        private string find_classe(string codice)
        {
            string codex = codice.Substring(0, 1);
            string ss = string.Empty;
            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    string s1 = "select * from classi where codice_classe = '";
                    cmd.CommandText = string.Concat(s1, codex, "'");
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            ss = reader.GetString(1).ToString();
                        }
                    }
                    conn.Close();
                }
            }
            return (ss);

        }



    }
}
